package com.ruoyi.web.controller.prevention;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.PreventionPlan;
import com.ruoyi.system.service.PreventionPlanService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 隐患排查计划(PreventionPlan)表控制层
 * @since 2022-12-17 13:21:33
 */
@RestController
@RequestMapping("/preventionPlan")
public class PreventionPlanController extends BaseController {

    @Resource
    private PreventionPlanService preventionPlanService;

    /**
     * 分页查询
     * @param preventionPlan 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(PreventionPlan preventionPlan) {
        startPage();
        List<PreventionPlan> preventionPlans = preventionPlanService.queryByPage(preventionPlan);
        return getDataTable(preventionPlans);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.preventionPlanService.queryById(id));
    }

    /**
     * 新增数据
     * @param preventionPlan 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody PreventionPlan preventionPlan) {
        return AjaxResult.success(this.preventionPlanService.insert(preventionPlan));
    }

    /**
     * 编辑数据
     * @param preventionPlan 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody PreventionPlan preventionPlan) {
        return AjaxResult.success(this.preventionPlanService.update(preventionPlan));
    }

    /**
     * 删除数据
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.preventionPlanService.deleteById(id));
    }


    /**
     * 巡检审核
     * @return
     */
    @PostMapping("/updatePreventionPlanState")
    public AjaxResult updatePreventionPlanState(@RequestBody PreventionPlan preventionPlan) {
        preventionPlanService.updatePreventionPlanState(preventionPlan);
        return AjaxResult.success();
    }


    /**
     *  导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "排查计划", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<PreventionPlan> util = new ExcelUtil<>(PreventionPlan.class);
        List<PreventionPlan> preventionPlans = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = preventionPlanService.importExcel(preventionPlans, updateSupport, operName);
        return success(message);
    }

    /**
     * 导出
     * @param response
     * @param preventionPlan
     */
    @Log(title = "排查计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreventionPlan preventionPlan) {
        List<PreventionPlan> list = preventionPlanService.queryList(preventionPlan);
        ExcelUtil<PreventionPlan> util = new ExcelUtil<>(PreventionPlan.class);
        util.exportExcel(response, list, "排查计划");
    }

    /**
     *  导入——模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<PreventionPlan> util = new ExcelUtil<>(PreventionPlan.class);
        util.importTemplateExcel(response, "排查计划");
    }


}

